<template>
  <div class="choice-container">

    <div class="card" style="margin-bottom: 5px;">
      <el-input v-model="data.name" style="width: 300px; margin-right: 10px" placeholder="请输入课程名称查询"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px">
      <el-table :data="data.tableData" stripe>
        <el-table-column label="课程名称" prop="name"></el-table-column>
        <el-table-column label="授课教师" prop="teacherName"></el-table-column>
        <el-table-column label="选课学生" prop="studentName"></el-table-column>
        <el-table-column label="操作" align="center" width="160" v-if="data.user.role === 'STUDENT'">
          <template #default="scope">
            <el-button type="danger" @click="handleDelete(scope.row.id)">取消选课</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card">
      <el-pagination background layout="prev, pager, next" v-model:page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
    </div>

  </div>
</template>

<script setup>
import request from "@/utils/request";
import {reactive} from "vue";
import {ElMessageBox, ElMessage} from "element-plus";

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}'),
  pageNum: 1,
  pageSize: 10,
  total: 0,
  form: {},
  tableData: [],
  name: null
})

// 分页查询
const load = () => {
  let teacherId = null
  let studentId = null
  if (data.user.role === 'TEACHER') {
    teacherId = data.user.id
  }
  if (data.user.role === 'STUDENT') {
    studentId = data.user.id
  }
  request.get('/choice/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name: data.name,
      teacherId: teacherId,
      studentId: studentId
    }
  }).then(res => {
    data.tableData = res.data?.list
    data.total = res.data?.total
  })
}

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm('取消后可能再也选不到这门课了哦?', '取消选课', { type: 'warning' }).then(res => {
    request.delete('/choice/deleteById/' + id).then(res => {
      if (res.code === '200') {
        load()
        ElMessage.success('操作成功')
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(err => {})
}

// 重置
const reset = () => {
  data.name = null
  load()
}

load()
</script>

<style scoped>
.choice-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.05);
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.card:hover {
  box-shadow: 0 4px 16px 0 rgba(0,0,0,.08);
}

/* 搜索区域样式 */
.search-area {
  display: flex;
  gap: 15px;
  align-items: center;
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  background-color: #fff;
  border-radius: 4px;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #1e88e5 inset;
}

/* 表格样式优化 */
:deep(.el-table) {
  border-radius: 8px;
  --el-table-border-color: #ebeef5;
  --el-table-header-bg-color: #f8f9fa;
  --el-table-row-hover-bg-color: #f0f7ff;
}

:deep(.el-table th) {
  font-weight: 600;
  color: #333;
  background-color: #f8f9fa;
}

:deep(.el-table td) {
  padding: 12px 0;
}

/* 课程名称列样式 */
.course-name {
  color: #1e88e5;
  font-weight: 500;
  transition: all 0.3s ease;
}

.course-name:hover {
  color: #42a5f5;
}

/* 教师名称列样式 */
.teacher-name {
  color: #666;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
  background-color: #f0f4f8;
  display: inline-block;
}

/* 学生名称列样式 */
.student-name {
  color: #666;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
  background-color: #f3e5f5;
  display: inline-block;
}

/* 按钮样式 */
:deep(.el-button--primary) {
  background: linear-gradient(135deg, #1e88e5 0%, #42a5f5 100%);
  border: none;
  transition: all 0.3s ease;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(30, 136, 229, 0.3);
}

:deep(.el-button--danger) {
  background: linear-gradient(135deg, #ff4d4f 0%, #ff7875 100%);
  border: none;
  transition: all 0.3s ease;
}

:deep(.el-button--danger:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 77, 79, 0.3);
}

:deep(.el-button--info) {
  background: linear-gradient(135deg, #909399 0%, #c0c4cc 100%);
  border: none;
  color: white;
  transition: all 0.3s ease;
}

:deep(.el-button--info:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(144, 147, 153, 0.3);
}

/* 分页器样式 */
:deep(.el-pagination) {
  margin-top: 20px;
  justify-content: flex-end;
}

:deep(.el-pagination .el-pagination__total) {
  font-size: 14px;
}

:deep(.el-pagination .btn-prev),
:deep(.el-pagination .btn-next) {
  background: transparent;
  border: none;
}

:deep(.el-pagination .el-pager li) {
  border-radius: 4px;
  transition: all 0.3s ease;
}

:deep(.el-pagination .el-pager li.active) {
  background-color: #1e88e5;
}
</style>